/**
 * 检查用户是否登录及处理
 */
function checkLoginStatusDeal(){
	var storage = window.localStorage;
	if(storage){
		if(storage.getItem($$.sysFlag+"hgtg-token")){
			$("#loginUser").html(storage.getItem($$.sysFlag+"hgtg-userName")+"，您好！");
			$("#system-name").html("超级模板系统");
		}else{
			$$.goUrl("/login/login.html");
		}
	}
	//获取菜单参数
	var pageParams=$$.getQueryString();
	window.menuCode = pageParams.topMenuCode;
	window.thirdMenuCode = pageParams.menuCode;
}

/**
 * 成功设置顶部菜单后处理
 */
function dealAfterSetMenuData(){
	//定义点击切换
	$(".q-first-nav li").click(function (){
		checkLoginStatusDeal();
		var selectTopMenu = $(this);
        $(".q-first-nav li[menuCode='"+selectTopMenu.attr("menuCode")+"']").addClass('li-active').siblings().removeClass('li-active');
    	$(".q-second-nav li[menuCode='"+selectTopMenu.attr("menuCode")+"']").addClass('li-active').siblings().removeClass('li-active');
    	//初始化左侧菜单
    	initSiderbarDataByMenuId(selectTopMenu.attr("menuId"),selectTopMenu.attr("menuCode"));
    	if(window.menuCode=="indexPage"){//点击首页其他顶部菜单
    		$(".wrapper").show();
    		$(".indexContent").hide();
    		$("#top-menu-name").html(selectTopMenu.attr("menuName"));
    	}
    });
    //定义点击切换
	$(".q-second-nav li").click(function (){
		checkLoginStatusDeal();
        var selectTopMenu = $(this);
        $(".q-first-nav li[menuCode='"+selectTopMenu.attr("menuCode")+"']").addClass('li-active').siblings().removeClass('li-active');
    	$(".q-second-nav li[menuCode='"+selectTopMenu.attr("menuCode")+"']").addClass('li-active').siblings().removeClass('li-active');
    	//初始化左侧菜单
    	initSiderbarDataByMenuId(selectTopMenu.attr("menuId"),selectTopMenu.attr("menuCode"));
    	if(window.menuCode=="indexPage"){//点击首页其他顶部菜单
    		$(".wrapper").show();
    		$(".indexContent").hide();
    		$("#top-menu-name").html(selectTopMenu.attr("menuName"));
    	}
    });
    if(window.menuCode){
    	$(".q-first-nav li[menuCode='"+window.menuCode+"']").click();
    }
}

/**
 * 设置远程顶部菜单数据
 */
function setRemoteMenuData(){
	//获取顶部菜单
	$$.ajax({
        url:"/rest/BaseMenuBusiness/getUserTopMenu",
        suiteCode:"base",
        data : {},
        success : function(data){
        	var topMenuHtml = "";
            if(data && data.length>0){
            	for(var i=0;i<data.length;i++){
            		var menuData = data[i];
            		var menuId = menuData.menuId;
            		var menuCode = menuData.menuCode;
            		var menuName = menuData.menuName;
            		topMenuHtml += "<li menuId='"+menuId+"' menuCode='"+menuCode+"' menuName='"+menuName+"'><p>"+menuName+"</p></li>";
            	}
            }
            $("#top-menu").html(topMenuHtml);
            $("#top-menu-second").html(topMenuHtml);
            dealAfterSetMenuData();
            var storage = window.localStorage;
			if(storage){
				storage.setItem($$.sysFlag+"hgtg-topMenuHtml",topMenuHtml);
			}
        }
   	});
}

/**
 * 初始化顶部菜单
 */
function initMenu(){
	var storage = window.localStorage;
	if(storage){
		var topMenuHtml = storage.getItem($$.sysFlag+"hgtg-topMenuHtml");
		if(topMenuHtml){
			$("#top-menu").html(topMenuHtml);
			$("#top-menu-second").html(topMenuHtml);
			dealAfterSetMenuData();
		}else{
			setRemoteMenuData();
		}
	}else{
		setRemoteMenuData();
	}
}

/**
 * 退出成功，清除本地缓存信息
 */
function loginOutSuccessDeal(){
	var storage = window.localStorage;
	if(storage){
		var userCode = storage.getItem($$.sysFlag+"hgtg-userCode");
		storage.clear();
		if(userCode){
			storage.setItem($$.sysFlag+"hgtg-userCode",userCode);
		}
	}
}

function logout(){
	$$.ajax({
        url:"/rest/BaseAuthBusiness/logout",
        suiteCode:"base",
        data : {},
        success : function(data){
        	loginOutSuccessDeal();
            $$.goUrl("/login/login.html");
        }
    });
}

$(function (){
	
	checkLoginStatusDeal();
	
	//初始化菜单
	initMenu();
	
	//退出登录
    $("#logout").on('click',function () {
        logout();
    });
    
});
